package com.wyh.service.serviceImpl.feemanagementserviceimpl;

import cn.hutool.core.collection.CollUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.wyh.domain.po.feemanagementpo.BillingItemss;
import com.wyh.domain.po.feemanagementpo.Contract;
import com.wyh.domain.po.feemanagementpo.Customers;
import com.wyh.domain.po.feemanagementpo.Resourcees;
import com.wyh.mapper.feemanagementmapper.ContractMapper;
import com.wyh.service.feemanagementservice.BillService;
import com.wyh.service.feemanagementservice.IContractService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.wyh.service.feemanagementservice.ICustomerService;
import com.wyh.service.feemanagementservice.IResourceService;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.stream.Collectors;

/**
 * <p>
 * 合同表 服务实现类
 * </p>
 *
 * @author ggt
 * @since 2024-05-29
 */
@Service
@RequiredArgsConstructor
public class ContractServiceImpl extends ServiceImpl<ContractMapper, Contract> implements IContractService {

    private final IResourceService resourceService;

    private final ICustomerService customerService;

    private final BillService billService;
    private final ContractMapper contractMapper;

    @Override
    public List<Resourcees> getResorceByCustId(Integer customId)  {
        //判断customerid是否存在
        List<Contract> contracts = lambdaQuery()
                .eq(Contract::getCustomerid, customId).list();
        if(contracts == null || contracts.isEmpty()){
            return Collections.emptyList();
        }
        //通过客户id查询到对应的所有合同

        //从合同集合中取出资源id
        List<Integer> resourceIds = contracts.stream().map(Contract::getResourceid).collect(Collectors.toList());
        //根据资源id查询资源信息
        List<Resourcees> resourcees = resourceService.listByIds(resourceIds);
        if(resourcees == null || resourcees.isEmpty()){
            return Collections.emptyList();
        }
        return resourcees;
    }

    //ContractServiceImpl
    @Override
    public List<Customers> getCustomerByResourceId(Integer resourceId) {
        //判断资源是否合法
        Resourcees resourcees = resourceService.getById(resourceId);
        if(resourcees == null || resourcees.getState() == 0){
           throw new RuntimeException("该资源不存在或者该资源未接房");
        }
        //根据资源id查询到对应的所有合同
        List<Contract> contracts = lambdaQuery().eq(Contract::getResourceid, resourceId).list();
        List<Integer> customerIds = contracts.stream().map(Contract::getCustomerid).collect(Collectors.toList());

        //根据客户id查询到对应的所有客户
        return customerService.listByIds(customerIds);
    }

    @Override
    public List<Customers> getCustomerByResourceIdPage(Integer resourceId, Integer page, Integer size) {
        //判断资源是否合法
        Resourcees resourcees = resourceService.getById(resourceId);
        if(resourcees == null || resourcees.getState() == 0){
            throw new RuntimeException("该资源不存在或者该资源未接房");
        }
        //根据资源id查询到对应的所有合同
        List<Contract> contracts = lambdaQuery().eq(Contract::getResourceid, resourceId).list();
        List<Integer> customerIds = contracts.stream().map(Contract::getCustomerid).collect(Collectors.toList());

        Page<Customers> page1 = customerService.page(new Page<>(page, size), new QueryWrapper<Customers>().in("id", customerIds));

        return page1.getRecords();
    }


    @Override
    public List<Contract> getContractByResourceId(Integer resourceId) {
        List<Contract> contracts = lambdaQuery().eq(Contract::getResourceid, resourceId).list();
        if(CollUtil.isEmpty(contracts)){
            return Collections.emptyList();
        }
        return contracts;
    }

    @Override
    public Contract getContractByCustomerIdAndResource(Integer customerId,Integer resourceId) {
        return lambdaQuery().eq(Contract::getCustomerid, customerId)
                .eq(Contract::getResourceid, resourceId)
                .one();
    }

    //添加费项
    @Override
    public List<BillingItemss> getItemsContractId(Integer contractId) {
        List<Integer> itemids = contractMapper.getItemidByContractId(contractId);
        //根据itemids查到周期性计费的计费项目
        List<BillingItemss> billingitems = contractMapper.getBillingitemsByIds(itemids);
        if(CollUtil.isEmpty(billingitems)){
            return Collections.emptyList();
        }
        return billingitems;
    }

    @Override
    public Integer getHomeownerByResourceId(Integer resourceId) {
        Contract contract = lambdaQuery()
                .eq(Contract::getResourceid, resourceId)
                .eq(Contract::getRole, 0)
                .one();
        return contract.getCustomerid();
    }

    @Override
    public List<Resourcees> getResorceByCustId(List<Integer> customIdList) {
        List<Resourcees> resourceesList = new ArrayList<>();
        for (Integer id : customIdList) {
            List<Resourcees> resorceByCustId = getResorceByCustId(id);
            resourceesList.addAll(resorceByCustId);
        }
        return resourceesList;
    }




}
